

package com.fan.util;

import com.fan.tank.EnemyTank;
import com.fan.tank.Tank;

import java.util.ArrayList;
import java.util.List;

public class EnemyTanksPool {
    public static final int DEFAULT_POOL_SIZE = 20;
    public static final int POOL_MAX_SIZE = 20;
    private static List<Tank> pool = new ArrayList<>();
    static {
        for (int i = 0; i < DEFAULT_POOL_SIZE; i++) {
            pool.add(new EnemyTank());
        }
    }
    //从池塘获取一个对象
    public static Tank get(){
        Tank tank ;
        //池塘被掏空
        if(pool.size() == 0){
            tank =new EnemyTank() {
            };
        }
        //还有对象
        else{
            tank = pool.remove(0);
        }
        return tank;
    }

    //子弹被销毁的时候，归还到池塘中
    public static void theReturn(Tank tank){
        //池塘的子弹个数达到最大值，那就不再归还
        if(pool.size() == POOL_MAX_SIZE){
            return;
        }
        pool.add(tank);
    }
}
